﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JMT.Controls.PopWindows
{
    /// <summary>
    /// PopWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PopWindow : UserControl
    {
        IPopupContent _popContent;

        public PopWindow()
        {
            InitializeComponent();
            this.Focusable = true;
        }

        public void Pop(UserControl content)
        {
            _ctlPanel.Children.Add(content);
            _popContent = content as IPopupContent;

            this.Visibility = System.Windows.Visibility.Visible;
            (Application.Current.MainWindow.Content as Grid).Children.Add(this);
            this.Focus();
        }

        private void _ctlBtnOK_Click(object sender, RoutedEventArgs e)
        {
            if (_popContent != null)
            {
                _popContent.OnOK();
            }

            (Application.Current.MainWindow.Content as Grid).Children.Remove(this);
        }

        private void _ctlBtnCancel_Click(object sender, RoutedEventArgs e)
        {
            if (_popContent != null)
            {
                _popContent.OnCancel();
            }

            (Application.Current.MainWindow.Content as Grid).Children.Remove(this);
        }

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            if (_popContent != null)
            {
                _popContent.OnFoucs();
                e.Handled = true;
            }
            else
            {
                base.OnGotFocus(e);
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                _ctlBtnOK_Click(null, null);
                e.Handled = true;
            }
            else if (e.Key == Key.Escape)
            {
                _ctlBtnCancel_Click(null, null);
                e.Handled = true;
            }

            base.OnKeyDown(e);
        }
    }
}
